﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
//using API.EbayWebService;
using DB;
using eBay.Service.Call;
using eBay.Service.Core.Sdk;
using eBay.Service.Core.Soap;
using eBay.Service.Util;

namespace API
{
    public class EBaySeller
    {
        public string SigninUrl = "https://api.ebay.com/wsapi";
        public string Version = "863";
        public int Siteid = 0;
        public string AppID;
        public string DevId;
        public string AuthCert;
        public string EBayToken;

    }

    public class EBaySellers
    {
        public static EBaySeller QqSerller = new EBaySeller()
        {
            Siteid = 0,
            AppID = "MaiBeyf2e-9dbe-431e-bf18-e77ef8fbda6",
            DevId = "300d0097-22b9-4708-b327-eeb3c1609f90",
            AuthCert = "978976d5-2fcd-4c04-b5b2-f54386c57ad5",
            EBayToken = "AgAAAA**AQAAAA**aAAAAA**DtMWUw**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6AGkoKlC5SGoQydj6x9nY+seQ**eCQCAA**AAMAAA**kuay3rTRrXmmtY27DM9vp5xhV9e26n0CdH1ZqKq6KnoiL+OnVUxKoajjgLek+gV8+9hnHE8UHf5/T7mCvsdiT0Nd5MQQC+vUzEubQ3CW22wCKub8hzocx5dRLdFHlBBp4kMzloawucktpVHrmMXwYYPZM8qh+qYLpQ6KIBG4OyXOKWu78prCQCfMO3MVsu7+XMF9kByXD/Io04pfSK+LACXdCxcopicoiskCDQLev7JQZNwmLDl1aSZ7HQSGCkavbP6xY+HOQS2SE5ONM+bJ3Pd2Xdng8mRywmA7XB2JznQ1jsScQwlFQGmdyeB5QuGoFEsZucZMC2iKu779eV2qBfYE4GwcD3X1s1tOjuE1zHurNFHoA4LOpKltySC1wZbdTDs6e7pD0276so9eQ11KO+rA9vfUlBVRPTtt8BATKBt1DJ2yhVVa6A2gvahnDaoJiCtCBz1GotrQJ85m/8IeLAeudDRllwaEe2sn+QfXetL75zN5qktgkkoSGdnduFxrleUpEQ2ETw8SrVLXlP+x5cHwex5QdJY/XYvwFMv4hceBjcoak3o1JNxxAqD/VZApQtRo5dXRk0YWHUWRu3tCrtkbp07SQttmo5UjPIA1rNSpEyeK2SRaX0fgGvWDEoEYjopWrH7HZrz7rHvJLZXg9wu0nMMqA5GXXUcRy8BXSt4Vm/dbvUUNsMSCGj3KeBOjtR4s2myjfoHsaUrI3bxB6tlTBaaJwJVb+fjVp7xHSVoOyj8j5oYWdEbHqxyFh6U6"
        };
    }

    public class EbayApi
    {
        protected ApiContext APIContext;
        protected EbayApi(EBaySeller seller)
        {
            APIContext = GetApiContext(seller);
        }
        private ApiContext GetApiContext(EBaySeller eBaySeller)
        {
            if (APIContext == null)
            {
                APIContext = new ApiContext();
                //set Api Server Url
                APIContext.SoapApiServerUrl = eBaySeller.SigninUrl;
                //set Api Token to access eBay Api Server
                var apiCredential = new ApiCredential();
                apiCredential.eBayToken = eBaySeller.EBayToken;
                APIContext.ApiCredential = apiCredential;
                //set eBay Site target to US
                APIContext.Site = SiteCodeType.US;

                //set Api logging
                APIContext.ApiLogManager = new ApiLogManager();
                APIContext.ApiLogManager.ApiLoggerList.Add(new FileLogger("listing_log.txt", true, true, true));
                APIContext.ApiLogManager.EnableLogging = true;
            }
            return APIContext;
        }

    }

    public class EbayAddItemApi : EbayApi
    {
        readonly AddItemCall _call;
        public EbayAddItemApi(EBaySeller seller)
            : base(seller)
        {
            _call = new AddItemCall(APIContext);
        }

        public void AddItem(Product product)
        {
            _call.AddItem(GetAddItemRequestType(product));
        }

        protected ItemType GetAddItemRequestType(Product product)
        {
            var rtn = new ItemType();
            rtn.Title = product.Feature;
            rtn.StartPrice = new AmountType() {Value = Convert.ToDouble(product.Price), currencyID = CurrencyCodeType.USD};
            rtn.SKU = product.SKU + "_" + DateTime.Now.ToFileTimeUtc();
            rtn.ListingType = ListingTypeCodeType.Auction;
            return rtn;
        }
    }

    public class EbayGetItemApi : EbayApi
    {
        readonly GetItemCall _call;
        public EbayGetItemApi(EBaySeller seller)
            : base(seller)
        {
            _call = new GetItemCall(APIContext);
        }
        public void GetItem(string itemid)
        {
            var v = _call.GetItem(itemid,true);
        }
    }

    public class EbayGetSellerListApi : EbayApi
    {
        readonly GetSellerListCall _call;
        public EbayGetSellerListApi(EBaySeller seller)
            : base(seller)
        {
            _call = new GetSellerListCall(APIContext);
        }
        public ItemTypeCollection GetOnlineItemList()
        {
            _call.ApiRequest.EndTimeToSpecified = true;
            _call.ApiRequest.EndTimeTo = DateTime.Now.AddDays(50);
            _call.ApiRequest.EndTimeFromSpecified = true;
            _call.ApiRequest.EndTimeFrom = DateTime.Now;
            _call.DetailLevelList = new DetailLevelCodeTypeCollection() { DetailLevelCodeType.ReturnAll };
            _call.Pagination = new PaginationType();
            _call.Pagination.EntriesPerPageSpecified = true;
            _call.Pagination.EntriesPerPage = 100;
            _call.Pagination.PageNumberSpecified = true;
            _call.Pagination.PageNumber = 1;
            return _call.GetSellerList();
        }

    }
}
